package com.hsq.androidproject.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.entity.HotStars;
import com.hsq.androidproject.ui.ShowHotStartsActivity;
import com.hsq.androidproject.utils.glide.GlideCircleTransform;

import java.util.List;


/**
 * Created by lenovo on 2016/11/22.
 */

public class MyGridViewStartRankingAdapter extends BaseAdapter {        ////明星排行榜
    private List<HotStars.HotStarsBean> mData;
    private Context context;

    public MyGridViewStartRankingAdapter(List<HotStars.HotStarsBean> mData, Context context) {
        this.mData = mData;
        this.context = context;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_gridview, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.iv_img = (ImageView) convertView.findViewById(R.id.iv_img);
            viewHolder.iv_count = (ImageView) convertView.findViewById(R.id.iv_count);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.rl_click = (RelativeLayout) convertView.findViewById(R.id.rl_click);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final HotStars.HotStarsBean hotStarsBean = mData.get(position);
        String thumb = hotStarsBean.getThumb();
        Glide.with(context)
                .load(thumb)
                .transform(new GlideCircleTransform(context))//圆形
                //               .transform(new GlideRoundTransform(this, 10))//圆角
                .into(viewHolder.iv_img);

        String name = hotStarsBean.getName();
        viewHolder.tv_name.setText(name);

        int count = hotStarsBean.getRank();
        if (count == 1) {
            viewHolder.iv_count.setImageResource(R.drawable.rank_1_icon);
        } else if (count == 2) {
            viewHolder.iv_count.setImageResource(R.drawable.rank_2_icon);
        } else if (count == 3) {
            viewHolder.iv_count.setImageResource(R.drawable.rank_3_icon);
        } else if (count == 4) {
            viewHolder.iv_count.setImageResource(R.drawable.rank_4_icon);
        } else if (count == 5) {
            viewHolder.iv_count.setImageResource(R.drawable.rank_5_icon);
        } else if (count == 6) {
            viewHolder.iv_count.setImageResource(R.drawable.rank_6_icon);
        } else if (count == 7) {
            viewHolder.iv_count.setImageResource(R.drawable.rank_7_icon);
        }

        viewHolder.rl_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = hotStarsBean.getName();
                String baileUrl = hotStarsBean.getBaikeUrl();
                String thumb = hotStarsBean.getThumb();
                int sid = hotStarsBean.getSid();
                int rank = hotStarsBean.getRank();
                int fansCount = hotStarsBean.getFansCount();
                Intent intent = new Intent(context, ShowHotStartsActivity.class);        ////展示每个明星的信息
                intent.putExtra("name", name);
                intent.putExtra("baileUrl", baileUrl);
                intent.putExtra("thumb", thumb);
                intent.putExtra("sid", sid);
                intent.putExtra("rank", rank);
                intent.putExtra("fansCount", fansCount);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    class ViewHolder {
        public ImageView iv_img, iv_count;
        private TextView tv_name;
        private RelativeLayout rl_click;
    }
}
